# filter companies for blue-green deployment in S4 region

=begin

files:
proxy_access.log.2020-08-13.gz

bash commands:

# cs_main service:
zgrep -vE '("kunapi\.s4\.udesk\.cn")|("baseqywx\.s4\.udesk\.cn")|("volcano\.s4\.udesk\.cn")' proxy_access.log.2020-08-13.gz | grep -Eo ' "[[:digit:]]+" ' | grep -Eo '[[:digit:]]+' > /mnt/tmp/tids_stat_08_13.log

wc -l /mnt/tmp/tids_stat_08_13.log   # 1775508

sort -n /mnt/tmp/tids_stat_08_13.log | uniq -c | sort -nr > /mnt/tmp/tids_freq.log

=end


def get_candidates(file_name: , total: )
  freq_by_tid = File.foreach(file_name).each_with_object({}) do |line, h|
    v, k = line.chomp.lstrip.split(" ").map(&:to_i)
    h[k] = v
  end

  freq_by_tid = freq_by_tid.reject { |k, _| k.nil? }

  ratio_by_tid = freq_by_tid.each_with_object({}) do |(k, v), h|
    h[k] = v.fdiv(total)
  end

  hash = ratio_by_tid.select { |_, v| v > 0.0001 }
  hash = hash.reject { |k, _| k == 0 }
  candidates = hash.reject { |k, _| block_ids.include?(k) }

  detailed_candidates = candidates.each_with_object({}) do |(tid, ratio), h|
    on_shard_by_company_id(tid) do
      com = Company.find(tid)
      h[tid] = {}
      h[tid]["subdomain"] = com.subdomain
      h[tid]["ratio"] = ratio * 100
      h[tid]["shard_id"] = ActiveRecord::Base.current_shard_id
      h[tid]["company_name"] = com.company_name
      h[tid]["agent_count"] = com.agents.count
      h[tid]["tid"] = tid
    end
  end

  de = detailed_candidates.select { |_, v| v['agent_count'] < 50 }
  de = de.select { |_, v| v['ratio'] > 0.02 }

  de.values.each do |h|
    puts "#{h['tid']},#{h['subdomain']},#{h['shard_id']},#{h['company_name']},#{h['ratio']},#{h['agent_count']}"
  end

  de   # de.values.map { |h| h['ratio'] }.inject(:+)  => 95.5

  first_50_percent_tenants = select_first_50_percent(de)

  puts "First 50 percent:"
  print_list(first_50_percent_tenants)
end

def select_first_50_percent(de)
  results = []
  de.reject { |k, _| huge_traffic_ids.include?(k) }.values.each do |h|
    if results.inject(0) { |r, h| r + h["ratio"] } <= 50
      results << h
    end
  end
  results
end

def print_list(array)
  puts
  puts "----"

  array.each do |h|
    puts "#{h['tid']},#{h['subdomain']},#{h['shard_id']},#{h['company_name']},#{h['ratio']},#{h['agent_count']}"
  end

  puts "----"
  puts
end

def key_customer_subdomains
  []
end

def key_ids
  # on_shard(1) { Company.where(subdomain: key_customer_subdomains).pluck(:id) }
  []
end

def special_ids
  [1]
end

def block_ids
  key_ids + special_ids
end

def huge_traffic_ids
  # 13640, 1361514, 13.2% traffic ratio, 上海韩众网络科技有限公司/上海摄图
  # 4874, dzyxedu, 5.66% traffic ratio, 河南高辉教育科技有限公司/河南省华辉计算机
  # [13640, 4874]
  [2592]
end

run(file_name: "/tmp/freq.log", total: 1775508)
